<?php

/**
 * Description of Comment
 *
 * @author Scott McKay
 */
class Application_Model_Comment extends Zend_Db_Table_Abstract {

    protected $_name = 'Comment';

    /**
     * Returns the Comment and the grade from the specified comment
     * 
     * @param The ID of the comment that you want information from
     */
    public function getComment($id) {
        $sql = "SELECT Comment, Grade FROM Comment WHERE Comment_ID = ?";
        $params = array($id);
        return $this->getAdapter()->query($sql, $params)->fetchAll();
    }

    public function getLastComments() {
        $sql = "SELECT Comment.Comment, Movie.Title, Customer.Login
FROM  Comment, Movie, Watches, Customer
WHERE
Comment.Comment_ID = Watches.Comment_ID AND
Movie.Movie_ID = Watches.Movie_ID AND
Customer.Customer_ID = Watches.Customer_ID
ORDER BY Comment.Comment_ID DESC
LIMIT 0 , 5";
        return $this->getAdapter()->query($sql)->fetchAll();
    }
    
    /**
     * This function stores a new comment on the database
     * 
     * @param Comment The comment to store
     * @param Grade The user assigned grade for the movie
     */
    public function addComment($Comment, $Grade){
		$sql = "INSERT INTO Comment (Comment_ID, Comment, Grade) VALUES (NULL, :comment, :grade);";
		$params = array(":comment" => $Comment, ":grade" => $Grade);
		$this->getAdapter()->query($sql, $params);

		$sql = "SELECT Comment_ID FROM Comment ORDER BY Comment_ID DESC LIMIT 0,1;";
		$values = $this->getAdapter()->query($sql)->fetchAll();
		return $values[0]["Comment_ID"];
	}
	
	public function deleteComment($id) {
		$sql = "DELETE FROM Comment WHERE Comment_ID = ?";
		$params = array($id);
		$this->getAdapter()->query($sql, $params);
	}

}

?>
